import service from './config.service'
class common{
    static rpxToPx(rpx, screenWidth = 750) {
        return rpx * (screenWidth / 750);
    }
    static exit(){
        uni.showModal({
            title: '提示',
            content: '确定退出吗？',
            success: (res) => {
                if (res.confirm) {
                    try {
                        uni.clearStorageSync();
                    } catch (e) {
                        console.log('清除失败');
                    }
                    setTimeout(() => {
                        common.go(3,'/pages/index/index');
                    },1000)
                } else if (res.cancel) {
                    console.log('用户点击取消');
                }
            }
        });

    }
    static back(){
        uni.navigateBack();
    }
    static go(type,path){
        switch (type) {
            case 1:
                uni.navigateTo({
                    url:path
                });
                break;
            case 2:
                //关闭当前页面，跳转到应用内的某个页面
                uni.redirectTo({
                    url:path
                });
                break;
            case 3:
                //关闭所有页面，打开到应用内的某个页面。
                uni.reLaunch({
                    url:path
                });
                break;
            case 4:
                uni.switchTab({
                    url:path
                });
                break;
            case 5:
                if (!uni.getStorageSync('token')){
                    uni.showModal({
                        title: '提示',
                        content: '未登录，请先登录',
                        success: (res) => {
                            if (res.confirm) {
                                common.go(1,'/pages/login/index');
                            } else if (res.cancel) {
                                console.log('用户点击取消');
                            }
                        }
                    });
                } else if(uni.getStorageSync('applyInfo')){
                    if(uni.getStorageSync('applyInfo').merchant_apply_status == -1){
                        return uni.showModal({
                            title: '提示',
                            content: '您还未商家，请先入驻商家',
                            success: (res) => {
                                if (res.confirm) {
                                    common.go(2,'/pages/apply/index');
                                } else if (res.cancel) {
                                    console.log('用户点击取消');
                                }
                            }
                        });
                    } else if(uni.getStorageSync('applyInfo').merchant_apply_status == 0){
                        return uni.showModal({
                            title: '提示',
                            content: '您入驻的商家正在审核',
                            success: (res) => {
                                if (res.confirm) {
                                    common.go(2,'/pages/apply/index');
                                } else if (res.cancel) {
                                    console.log('用户点击取消');
                                }
                            }
                        });
                    } else if(uni.getStorageSync('applyInfo').merchant_apply_status == 1){
                        common.go(1,path);
                    } else if(uni.getStorageSync('applyInfo').merchant_apply_status == 2){
                        return uni.showModal({
                            title: '提示',
                            content: '您入驻的商家审核未通过，请重新提交',
                            success: (res) => {
                                if (res.confirm) {
                                    common.go(2,'/pages/apply/index');
                                } else if (res.cancel) {
                                    console.log('用户点击取消');
                                }
                            }
                        });
                    }
                } else{
                    common.go(1,path);
                }
                break;
        }
    }
    static formatDate(value) {
        let date = new Date(value);
        //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? ('0' + MM) : MM; //月补0
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d; //天补0
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h; //小时补0
        let m = date.getMinutes();
        m = m < 10 ? ('0' + m) : m; //分钟补0
        let s = date.getSeconds();
        s = s < 10 ? ('0' + s) : s; //秒补0
        let time = '';
        time = y + '-' + MM + '-' + d + ' ' + h + ':'+ m; //年月日
        return time
    }
    static formatTimestamp(timestamp) {
        const now = new Date();
        const date = new Date(timestamp);

        // 获取今天的日期（去掉时分秒）
        const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());

        // 获取昨天的日期
        const yesterday = new Date(today);
        yesterday.setDate(today.getDate() - 1);

        // 获取前天的日期
        const dayBeforeYesterday = new Date(today);
        dayBeforeYesterday.setDate(today.getDate() - 2);

        // 判断是否是今天
        if (date >= today) {
            return `今天 ${common.formatTime(date)}`;
        }
        // 判断是否是昨天
        else if (date >= yesterday) {
            return `昨天 ${common.formatTime(date)}`;
        }
        // 判断是否是前天
        else if (date >= dayBeforeYesterday) {
            return `前天 ${common.formatTime(date)}`;
        }
        // 更早的日期
        else {
            return common.formatDateTwo(date);
        }
    }

    static formatTime(date) {
        const hours = date.getHours().toString().padStart(2, '0');
        const minutes = date.getMinutes().toString().padStart(2, '0');
        return `${hours}:${minutes}`;
    }

    static formatDateTwo(date) {
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, '0');
        const day = date.getDate().toString().padStart(2, '0');
        return `${year}-${month}-${day}`;
    }

    static formatTimeChat(time) {
        const date = new Date(time);
        const now = new Date();
        const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); // 今天的零点
        const yesterday = new Date(today);
        yesterday.setDate(today.getDate() - 1); // 昨天的零点

        // 格式化时间为两位数
        const formatTwoDigits = (num) => (num < 10 ? `0${num}` : num);

        // 获取时分
        const hours = formatTwoDigits(date.getHours());
        const minutes = formatTwoDigits(date.getMinutes());

        if (date >= today) {
            // 如果是今天，返回时分
            return `${hours}:${minutes}`;
        } else if (date >= yesterday) {
            // 如果是昨天，返回昨天 + 时分
            return `昨天 ${hours}:${minutes}`;
        } else {
            // 如果是更早的时间，返回年月日时分
            const year = date.getFullYear();
            const month = formatTwoDigits(date.getMonth() + 1);
            const day = formatTwoDigits(date.getDate());
            return `${year}-${month}-${day} ${hours}:${minutes}`;
        }
    }

    static upLoadImg(url){
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                // url: `http://ccc.healthspringny.com/api/common/image_upload`, // 仅为示例，非真实的接口地址
                url: `https://www.cccshansong.com/api/common/image_upload`, // 仅为示例，非真实的接口地址
                filePath: url,
                name: 'file',
                header:{
                    // Authorization:'Bearer ' + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2NjYy5oZWFsdGhzcHJpbmdueS5jb20vYXBpL2NvbnN1bWVyL3Bhc3N3b3JkTG9naW4iLCJpYXQiOjE3NDA3NTM2MTgsImV4cCI6MTc0NTkzNzYxOCwibmJmIjoxNzQwNzUzNjE4LCJqdGkiOiJ4a0x4T1BhTzhQNEhSNjREIiwic3ViIjoiMSIsInBydiI6ImMxYjY1YjFmMGQ2OGE2OWEwYjFjZjI5NjczZDI4NTRiYzBmMjk5M2QifQ.NEA11jSJ3XkjW3wXSZupCCnuLCFkitUrs_me-rkDmyA'
                    Authorization:'Bearer ' + uni.getStorageSync('token')
                },
                success: (res) => {
                    console.log(res);
                    resolve(JSON.parse(res.data));
                },
                fail:(err) => {
                    reject(err);
                }
            });
        })
    }
}

export default common
